package com.example.mylibrary.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



import java.util.List;

/**
 * Created by 任聪 on 2018/1/4.
 */
    public abstract class RvAdapter<T> extends RecyclerView.Adapter<RvHolder> {
        protected List<T> list;
        protected Context mContext;
        protected RvListener listener;
        protected LayoutInflater mInflater;
        private RecyclerView mRecyclerView;

        public RvAdapter(Context context, List<T> list, RvListener listener) {
            mContext = context;
            mInflater = LayoutInflater.from(context);
            this.list = list;
            this.listener = listener;
        }
    public void notifyData(List<T> poiItemList) {
        if (poiItemList != null) {
            int previousSize = list.size();
            list.clear();
            notifyItemRangeRemoved(0, previousSize);
            list.addAll(poiItemList);
            notifyItemRangeInserted(0, poiItemList.size());
        }
    }
        @Override
        public RvHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = mInflater.inflate(getLayoutId(viewType), parent, false);
            return getHolder(view, viewType);
        }

        protected abstract int getLayoutId(int viewType);

        @Override
        public void onBindViewHolder(RvHolder holder, int position) {
            holder.bindHolder(list.get(position), position);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        @Override
        public int getItemViewType(int position) {
            return 0;
        }

        protected abstract RvHolder getHolder(View view, int viewType);

    }

